#include "matrix2.h"
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
#include <vector>

using std::vector;

class Toolbox2 : public QThread
{
    Q_OBJECT
private:
	char * mono;
	char * sequence;
	char * monoFile;
	char * searchFile;
        char * outFile;
	int mono_len;
	int sequence_len;
//	Array_align * best;
	float *matrix_vals;	//match, mismatch, gap in sequence, gap in monomer


	int threshold;
	int min_length;
	int max_length;
	char * results_tag;					//tag to label results taken from sequence file or user input
        int runMode; // run mono search(0) or seq search(1);
        QString qstatus;
        bool isPaused;
        QMutex mutex;
        QWaitCondition wcondition;

	bool set_matrix_values();
	bool get_parameters_set_search();
	bool get_parameters_free_search();
	bool set_threshold(int t);
	bool set_results_tag(char * tag);
	bool set_mins_maxs();
	bool get_range();
        bool search(vector<Align> & a);
	bool get_head(int & size, char *& val);
	bool get_tail(int & size, char *& val);
        bool get_locs(int *& head_locs, int & num_head_locs, int *& tail_locs, int & num_tail_locs, int & pivot_size);
        bool populateA(int *& h_locs, int num_h_locs, int *& t_locs, int num_t_locs, int pivot_size, vector<Align> & a);
        bool pruneA(vector<Align> & a, int buffer);
	bool get_file_out(char *& file_name);
	bool get_file_out_free(char *& file_name);

        bool overlap(vector<Align> &a, Align * element, int buffer);
        bool write_final_results(vector<Align> a, char * file);
        bool write_temp_results(vector<Align> a, char * file, int size);
        bool sortVector(vector<Align> &a);
        float get_sum_sim(vector<Align> & a);

protected:
        void run();

public:
        Toolbox2();
	Toolbox2(char * mono_file, char * sequence_file);
	Toolbox2(char * sequence_file);
	~Toolbox2();
        char header[300];
	int get_threshold();
	bool get_results_tag(char *& tag);
        int buffer;  // maximum overlap


	bool show_param_vals();
	bool show_matrix_values();
        bool set_matrix_values(float match, float mismatch, float gapSeq, float gapMono);

        void set_run_mode(int mode);
        void set_mono_search_params(char * mono_file, char * sequence_file, char *out_file, int thresh);
        void set_mono_discovery_params(char *sequence_file, char *out_file,
                                int thresh, int mono_min, int mono_max);
        void start_mono_search();
        void start_mono_discovery();
        void pauseThread();
        void unpauseThread();

signals:
        void calculationStarted();
        void setTags(QString &tag);
        void calculationFinished();
        void statusMessage(QString status);
        void resultsTag(QString rTag);
};
